﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bot.Application.Dto;
using Bot.Application.Services;
using Microsoft.AspNetCore.Mvc;

namespace Bot.WebAPI.Controllers
{
    [Route("api/v3/chatbot")]
    [ApiController]
    public class ResponsesController : ControllerBase
    {

        private readonly IResponseAppService _app;
        private readonly IGreetingMessageInChannelAppService _greetingMessageInChannelAppService;
        private readonly IIntentAnswerInChannelAppService _intentAnswerInChannelAppService;

        public ResponsesController(IResponseAppService app,
            IGreetingMessageInChannelAppService greetingMessageInChannelAppService,
            IIntentAnswerInChannelAppService intentAnswerInChannelAppService)
        {
            this._app = app;
            _greetingMessageInChannelAppService = greetingMessageInChannelAppService;
            _intentAnswerInChannelAppService = intentAnswerInChannelAppService;
        }

        [HttpGet]
        [Route("greetingMessageInChannels/{greetingMessageInChannelId:Guid}/responses")]
        public ActionResult<List<ResponseDto>> GetGreetingMessageResponses([FromRoute] Guid greetingMessageInChannelId, [FromQuery] string include)
        {
            var responses = _app.GetList(greetingMessageInChannelId, include);
            return Ok(responses);
        }

        [HttpGet]
        [Route("answerInChannels/{answerInChannelId:Guid}/responses")]
        public ActionResult<List<ResponseDto>> GetAnswerInChannelResponses([FromRoute] Guid answerInChannelId, [FromQuery] string include)
        {
            var responses = _app.GetList(answerInChannelId, include);
            return Ok(responses);
        }

        [HttpGet]
        [Route("responses/{id:Guid}")]
        public ActionResult<ResponseDto> Get([FromRoute] Guid id, [FromQuery] string include)
        {
            ResponseDto response = _app.Get(id, include); //include intent
            return Ok(response);
        }

        [HttpPost]
        [Route("greetingMessageInChannels/{greetingMessageInChannelId:Guid}/responses")]
        public ActionResult<List<ResponseDto>> CreateGreetingMessageResponse([FromRoute] Guid greetingMessageInChannelId, [FromBody] ResponseCreateDto dto)
        {
            ResponseDto response = _app.Add(greetingMessageInChannelId,dto);
            return CreatedAtAction(nameof(Get), new { id = response.Id }, response);
        }

        [HttpPost]
        [Route("answerInChannels/{answerInChannelId:Guid}/responses")]
        public ActionResult<List<ResponseDto>> CreateAnswerInChannelResponse([FromRoute] Guid answerInChannelId, [FromBody] ResponseCreateDto dto)
        {
            ResponseDto response = _app.Add(answerInChannelId,dto);
            return CreatedAtAction(nameof(Get), new { id = response.Id }, response);
        }

        [HttpPut]
        [Route("responses/{id:Guid}")]
        public ActionResult<ResponseDto> Put([FromRoute] Guid id, [FromBody] ResponseUpdateDto dto)
        {            
            ResponseDto response = _app.Update(id,dto);
            return Ok(response);
        }

        [HttpDelete]
        [Route("responses/{id:Guid}")]
        public ActionResult Delete([FromRoute] Guid id)
        {
             _app.Delete(id);
            return NoContent();
        }
    }
}
